package com.feisuanyz.controller;

import com.feisuanyz.dto.NotificationDTO;
import com.feisuanyz.dto.NotificationQuery;
import com.feisuanyz.dto.RestResult;
import com.feisuanyz.query.UpdateNotificationStatusQuery;
import com.feisuanyz.query.ViewNotificationQuery;
import com.feisuanyz.service.NotificationService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   通知管理控制器
 * </p>
 * @author 23265
 */
@RestController
@RequestMapping("/notifications")
@Slf4j
@Validated
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/{notificationId}")
    public ResponseEntity<RestResult> viewNotification(@PathVariable Integer notificationId) {
        ViewNotificationQuery query = new ViewNotificationQuery();
        query.setNotificationId(notificationId);
        try {
            NotificationDTO notificationDTO = notificationService.viewNotification(query);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", notificationDTO));
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestResult("000001", e.getMessage(), null));
        }
    }

    @PutMapping
    public ResponseEntity<RestResult> updateNotificationStatus(@Valid @RequestBody UpdateNotificationStatusQuery query, @RequestHeader("userId") Integer userId) {
        try {
            NotificationDTO notificationDTO = notificationService.updateNotificationStatus(query, userId);
            return ResponseEntity.ok(new RestResult("000000", "调用成功", notificationDTO));
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            if ("用户无权限".equals(e.getMessage())) {
                return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new RestResult("000001", e.getMessage(), null));
            } else if ("通知不存在".equals(e.getMessage())) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestResult("000001", e.getMessage(), null));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new RestResult("999999", e.getMessage(), null));
            }
        }
    }

    @PostMapping("/create")
    public ResponseEntity<RestResult<Void>> createNotification(@Valid @RequestBody NotificationQuery notificationQuery) {
        RestResult<Void> result = notificationService.createNotification(notificationQuery);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/edit")
    public ResponseEntity<RestResult<Void>> editNotification(@Valid @RequestBody NotificationQuery notificationQuery) {
        RestResult<Void> result = notificationService.editNotification(notificationQuery);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/delete/{notificationId}")
    public ResponseEntity<RestResult<Void>> deleteNotification(@PathVariable Integer notificationId, @RequestParam Integer deleteBy) {
        RestResult<Void> result = notificationService.deleteNotification(notificationId, deleteBy);
        return ResponseEntity.ok(result);
    }
}